<#assign menu="merchant_add">
<#include "/system/head.ftl">
<script language="javascript" src="http://webapi.amap.com/maps?v=1.3&key=0124fded525ed5c45b8bc81d43a0db5a"></script>

<body>
<section id="main-content">
    <section class="wrapper">
        <div class="tab-content">

            <div class="tab-pane active" id="tab_2">
                <div class="portlet box green">
                    <div class="portlet-title">
                        <div class="caption">
                            <i class="fa fa-gift"></i>新增商户
                        </div>
                    </div>

                    <div class="portlet-body form">
                        <!-- BEGIN FORM-->
                        <form onSubmit="return false;" id="addForm" method="post"
                              action="${basePath}/admin/merchant/add.json" class="form-horizontal">

                            <input type="hidden" name="credentials" id="credentials">

                            <div class="form-body">
                                <div>
                                    <h3 class="form-section">基础信息</h3>

                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label class="control-label col-md-3">商户名称
                                                    <span class="required" aria-required="true">* </span></label>

                                                <div class="col-md-9">
                                                    <input name="name" id="name" type="text" class="form-control"
                                                           placeholder="">
                                                </div>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label class="control-label col-md-3">商户类型
                                                </label>

                                                <div class="col-md-9">
                                                    <select name="merchantType" id="merchantType-select" class="form-control" data-val="">
                                                    <#list merchantCategoryList as e>
                                                        <option value=${e.id}>${e.name}</option>
                                                    </#list>
                                                    </select>


                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label class="control-label col-md-3">联系人
                                                    <span class="required" aria-required="true">* </span></label>

                                                <div class="col-md-9">
                                                    <input name="contact" type="text" class="form-control">

                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <#if "${auditShow}" = "yes">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label class="control-label col-md-3">通过审核
                                                        <span class="required" aria-required="true">* </span>
                                                    </label>

                                                    <div class="col-md-9">
                                                        <input type="radio" name="status" value="audit"> 通过
                                                        <input type="radio" name="status" value="noAudit">不通过
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                    </#if>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label class="control-label col-md-3">联系电话
                                                    <span class="required" aria-required="true">* </span></label>

                                                <div class="col-md-9">
                                                    <input name="contact_tel" type="text" class="form-control"
                                                           placeholder="">
                                                <#--<span class="help-block">-->
                                                <#--手机或者座机号 </span>-->
                                                </div>
                                            </div>
                                        </div>
                                        <!--/span-->
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label class="control-label col-md-3">座机号
                                                    <span class="required" aria-required="true">* </span></label>

                                                <div class="col-md-9">
                                                    <input name="fixed_tel" type="text" class="form-control"
                                                            >
                                                <#--<span class="help-block">-->
                                                <#--手机或者座机号 </span>-->
                                                </div>
                                            </div>
                                        </div>
                                        <!--/span-->
                                    </div>


                                    <!--/span-->
                                </div>


                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label col-md-3">商家照片
                                                <span class="required" aria-required="true">* </span></label>

                                            <div class="col-md-9">
                                                <div id="attachment"></div>
                                                <button id="file_upload" class="btn btn-shadow btn-info"
                                                        type="button">
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                    <!--/span-->
                                </div>


                            </div>

                            <!--/row-->
                        <#--<h3 class="form-section">营业执照</h3>-->





                            <div>
                                <h3 class="form-section">地址</h3>

                                <div class="row" id="city_2">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label col-md-3">省、市、区
                                                <span class="required" aria-required="true">* </span></label>

                                            <div class="col-md-9">
                                                <select name="province" class="prov   bs-select input-default"
                                                        data-style="btn-primary"></select>
                                                <select name="city" class="city   bs-select input-default"
                                                        disabled="disabled"
                                                        data-style="btn-primary"></select>
                                                <select name="area" class="dist   bs-select input-default"
                                                        disabled="disabled"
                                                        data-style="btn-primary"></select>
                                            </div>
                                        </div>
                                    </div>
                                    <!--/span-->
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label col-md-3">详细地址
                                                <span class="required" aria-required="true">* </span></label>

                                            <div class="col-md-9">
                                                <div class="input-group">


                                                    <input class="form-control" type="text" name="address"
                                                           id="address">

												<span class="input-group-btn">
												<button id="genpassword" class="btn btn-success" type="button"
                                                        onClick="geocoder()"><i class="fa fa-arrow-left fa-fw"></i>
                                                    获取经纬度
                                                </button>
												</span>
                                                </div>


                                            </div>
                                        </div>
                                    </div>

                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label col-md-3">纬度</label>

                                            <div class="col-md-9">
                                                <input name="latitude" type="text" class="form-control"
                                                       id="latitude" readonly>
                                            </div>
                                        </div>
                                    </div>
                                    <!--/span-->
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label col-md-3">经度</label>

                                            <div class="col-md-9">

                                                <input name="longitude" type="text" class="form-control"
                                                       id="longitude" readonly>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div>
                                <h3 class="form-section">商家简介</h3>

                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label col-md-3">商家描述</label>

                                            <div class="col-md-9">
                                                <textarea name="description" rows="10" cols="118"></textarea>


                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>

                            <div class="row">
                                <div class="form-actions fluid">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="col-md-offset-3 col-md-9">
                                                <button type="submit" class="btn green">提交</button>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
</section>
</body>


<script type="text/javascript">
    var kindId = "${kindId}";
    var kind = "article";
    $.extend({
        getAttachment: function () {
            $.getJSON("${basePath}/admin/attachment/list.json?kindId=" + kindId + "&v=" + Math.random(), function (data) {
                $('#attachment').html("");
                $.addAttachment(data.attachmentList);
            });
        },
        addAttachment: function (list) {
            console.log(list);
            var html = '<table class="table"><thead><tr><th>文件名</th><th>链接</th><th>大小</th><th>操作</th></tr></thead><tbody>';
            var urlList="";
            for (i = 0; i < list.length; i++) {
                var attachment = list[i];
                html += '<tr>';
                if (attachment.type == "photo") {
                    html += '<td>' + attachment.name + '</td>';
                } else {
                    html += '<td>' + attachment.name + '</td>';
                }
                html += '<td><a target="_blank" href="' + attachment.path + '" >' + attachment.path + '</a></td><td>' + attachment.size + '</td><td>';
                html += '<a href="javascript:void(0);" title="删除" name="' + attachment.name + '" class="btn btn-danger btn-xs js_delete" attachmentId="' + attachment.attachmentId + '">删除</a> ';
                html += '</td></tr>';
                urlList += attachment.path + ",";
            }

            $("#credentials").val(urlList);

            html += '</tbody></table>';
            $('#attachment').prepend(html);
            $('#attachment .js_delete').click(function () {
                var file = $(this);
                bootbox.confirm("是否要删除【" + $(this).attr("name") + "】文件？", function (result) {
                    if (result) {
                        $.post("${basePath}/admin/attachment/delete.json", {'attachmentId': file.attr("attachmentId")}, function (data) {
                            if (data.result) {
                                $.getAttachment();
                            }
                        }, "json");
                    }
                });
            });
            $('#attachment .js_status').click(function () {
                $.post("${basePath}/admin/attachment/update_status.json", {
                    'attachmentId': $(this).attr("attachmentId"),
                    'status': $(this).attr("status")
                }, function (data) {
                    if (data.result) {
                        $.getAttachment();
                    }
                }, "json");
            });
            $('#attachment .js_link').click(function () {
                var attachmentId = $(this).attr("attachmentId");
                bootbox.prompt("为此附件增加链接", function (result) {
                    if (result != "") {
                        $.post("${basePath}/admin/attachment/update_link.json", {
                            'attachmentId': attachmentId,
                            'link': result
                        }, function (data) {
                            if (data.result) {
                                $.getAttachment();
                            }
                        }, "json");
                    }
                });
            });
        }
    });
    function initFileUpload() {
        $('#file_upload').uploadify({
            'buttonText': '选择图片',
            'swf': '${basePath}/system/assets/uploadify/uploadify.swf',
            'uploader': '${basePath}/admin/attachment/upload.json;jsessionid=${JSESSIONID}',
            'formData': {'kindId': kindId, 'kind': kind},
            'fileObjName': 'file',
            'fileTypeExts': '*.*',
            'uploadLimit': '1000',
            'method': 'post',
            'onUploadSuccess': function (file, data, response) {
                $.getAttachment();
            }
        });
    }

    $(function () {
        $('#fileForm').ajaxForm({

            success: function (data) {
                if (data.result) {
                    bootbox.alert("保存成功");
                } else {
                    bootbox.alert(data.msg);
                }
            },
            error: function (data) {

            }
        });


//        $.getAttachment();
    });


</script>

<script language="javascript">
    var mapObj;
    var marker = new Array();
    var windowsArr = new Array();

    mapObj = new AMap.Map("iCenter");

    function geocoder() {
        var MGeocoder;
        console.log($("#name").val());
        //加载地理编码插件
        mapObj.plugin(["AMap.Geocoder"], function () {
            MGeocoder = new AMap.Geocoder({
                radius: 5000 //范围，默认：500
            });
            //返回地理编码结果
            AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack);
            //地理编码
            console.log($("#city_2 .prov").val() + $("#city_2 .city").val() + $("#city_2 .dist").val() + $("#address").val());
            console.log($("#city_2 .dist").val() == null)
            if ($("#city_2 .dist").val() == null) {
                MGeocoder.getLocation($("#city_2 .prov").val() + $("#city_2 .city").val() + $("#address").val());
            } else {
                MGeocoder.getLocation($("#city_2 .prov").val() + $("#city_2 .city").val() + $("#city_2 .dist").val() + $("#address").val());
            }

        });
    }
    //地理编码返回结果展示
    function geocoder_CallBack(data) {
        var resultStr = "";
        //地理编码结果数组
        var geocode = new Array();
        geocode = data.geocodes;
        console.log(data);
        $("#latitude").val(geocode[0].location.getLat());
        $("#longitude").val(geocode[0].location.getLng());

    }
</script>



<#include "/system/foot.ftl">

<script src="${basePath}/system/js/jquery.cityselect.js"></script>
<script>
    $("#city_2").citySelect({prov: "江苏", nodata: "none"});
</script>

<script>
    $('#addForm').validate({

        submitHandler: function (form) {
            $('#addForm').ajaxSubmit(function (data) {

                if (data.result) {

                    location.href = "/admin/merchant/page.htm";
                } else {
                    $('.alert-danger span', $('#addForm')).html(data.msg);
                    $('.alert-danger', $('#addForm')).show();
                }
            });
        }
    });
    initFileUpload();
</script>


<#--选择器初始化-->
<script>
    $("#merchantType-select").val("");
    initFileUpload();
</script>



